package com.siloam.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.transaction.annotation.Transactional;

import com.siloam.dao.UserDao;
import com.siloam.module.User;
import com.siloam.util.Cfg;
import com.siloam.util.EncryptUtil;
import com.siloam.vo.PageVO;
import com.siloam.vo.UserVO;

public class UserService {

	private UserDao userDao;
	
	private EncryptUtil encryptUtil;
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	@Transactional
	public void add(User user){
		user.setUsername( getNextUsername() );
		user.setPassword( getDefaultPwd() );
		user.setStatus(User.STATUS_STAY);
		userDao.insert(user);
	}
	
	public PageVO<UserVO> findPage(int rowBeginIndex, int rowCount){
		int total = userDao.getCount();
		List<User> users = userDao.findPage(rowBeginIndex, rowCount);
		
		PageVO<UserVO> pageVO = new PageVO<UserVO>();
		pageVO.setTotal(total);
		pageVO.setRows( toUserVO(users) );
		return pageVO;
	}
	
	private List<UserVO> toUserVO(List<User> users){
		List<UserVO> userVOs = new ArrayList<UserVO>();
		for(User user : users){
			userVOs.add( new UserVO(user) );
		}
		return userVOs;
	}
	
	public String getDefaultPwd(){
		return encryptUtil.encrypt(Cfg.defaultPwd);
	}
	
	/**
	 * 取得下一个用户的用户名
	 * 用户名由S+四位数字组成，如:S1001
	 * 
	 * 取得用户名最大的一个，取得后面四位数字，加一再加上前缀即可.
	 * @return
	 */
	private String getNextUsername(){
		String username = userDao.getMaxUsername();
		if(null == username){
			return  Cfg.unamePrefix + Cfg.unameStart;
		} else {
			String num = username.substring(Cfg.unamePrefix.length());
			return Cfg.unamePrefix + ( Integer.parseInt(num) + 1 );
		}
	}
	
	@Transactional
	public void update(User user){
		userDao.update(user);
	}
	
	public User load(int id){
		return userDao.load(id);
	}

	public EncryptUtil getEncryptUtil() {
		return encryptUtil;
	}

	public void setEncryptUtil(EncryptUtil encryptUtil) {
		this.encryptUtil = encryptUtil;
	}
	
	@Transactional
	public void delete(String ids){
		userDao.delete(ids);
	}
	
	@Transactional
	public void setStatus(String ids,int status){
		userDao.setStatus(ids, status);
	}
	
	public List<User> searchUser(String name, int type){
		if(type == 0){
			return userDao.searchUser(name);
		} else if(type == 1){
			return userDao.searchUserNoGroup(name);
		}
		return new ArrayList<User>();
	}
	
	/**
	 * 是否是系统管理员
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean isAdmin(String username, String password){
		InputStream is = getClass().getResourceAsStream("/admin.properties");
		Properties prop = new Properties();
		try {
			prop.load(is);
			String aname = prop.getProperty("username");
			String apwd = prop.getProperty("password");
			if(username.equals(aname) && encryptUtil.equals(password, apwd)){
				return true;
			}
		} catch (IOException e) {
			System.err.println("load admin prop error");
		}
		return false;
	}
	
	
}
